package com.easybuilder.core.im.netty;

import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;


public class IMClient {
    // ip地址
    private String host;
    // 端口
    private int port;
    // 客户端套接字
    public static Channel nioSocketChannel;

    // 构造方法，初始化套接字等数据
    public IMClient(String host, int port) {
        NioEventLoopGroup group = new NioEventLoopGroup();
        try {

            Bootstrap bootstrap = new Bootstrap();
            bootstrap.group(group)
                    .channel(NioSocketChannel.class)
                    .handler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel socketChannel) throws Exception {
                            socketChannel.pipeline().addLast(new IMClientWriteHandler());
                            System.out.println("socketChannel: "+ socketChannel);
                            // nioSocketChannel = socketChannel;
                        }
                    });
            ChannelFuture channelFuture = bootstrap.connect(host, port).sync();
            System.out.println("netty client connected!");
            channelFuture.channel().closeFuture().sync();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            group.shutdownGracefully();
        }
    }
    /** 发送数据 */
    public void send() {

    }

}

class ClientTest {

    public static void main(String[] args) {
        IMClient client = new IMClient("localhost", 8088);
//        new Thread(() -> {
//            client.send();
//        }).start();
    }
}